package LeetCode_Stack;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:用栈实现队列
 * User: 阿宾
 * Date: 2024-11-15
 * Time: 08:29
 */
/*
实现 MyQueue 类：

void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空，返回 true ；否则，返回 false
 */
public class Demo6 {
    static class MyQueue {

        private Stack<Integer> pushStack;
        private Stack<Integer> popStack;
        public MyQueue() {
            this.pushStack = new Stack<>();
            this.popStack = new Stack<>();
        }

        public void push(int x) {
            if(empty()){
                pushStack.push(x);
                return;
            }
            while (!popStack.empty()){
                pushStack.push(popStack.pop());
            }
            pushStack.push(x);
        }

        public int pop() {
            if(empty()){
                return -1;
            }
            //出队，将pushStack全部出栈到popStack中
            while(!pushStack.empty()){
                popStack.push(pushStack.pop());
            }
            return popStack.pop();

        }

        public int peek() {
            if(empty()){
                return -1;
            }
            //出队，将pushStack全部出栈到popStack中
            while(!pushStack.empty()){
                popStack.push(pushStack.pop());
            }
            return popStack.peek();
        }

        public boolean empty() {
            if(pushStack.empty() && popStack.empty()){
                return true;
            }
            return false;
        }
    }
}
